<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>

<head>

<script>

	var medicinePrescriptions = [];
	
/* 	$('#medicine').combobox({    
	    url:'${ctx}/medicine/list',    
	    valueField:'id',    
	    textField:'name'   
	});  */
	
</script>

<c:forEach items="${medicinePrescriptions}" var="medicinePrescription">
	<script type="text/javascript">
		var medicinePrescription = {
			'id':"${medicinePrescription.id}",
			'name': "${medicinePrescription.name}",
			'medicineNum':"${medicinePrescription.medicineNum}",
		};
		medicinePrescriptions.push(medicinePrescription);
	</script>
</c:forEach>

<script type="text/javascript">
debugger;
	$(function() {
		debugger;
		dataGrid = $('#dataGrid').datagrid({
			striped : true,
			rownumbers : true,
			idField : 'id',
			sortName : 'id',
			sortOrder : 'asc',
			columns :[[{
				width : '100',
				title : 'id',
				field : 'id'
			},{
				width : '100',
				title : '药品名称',
				field : 'name'
			}, {
				width : '80',
				title : '开药数量',
				field : 'medicineNum',
				editor:{
					type:'numberbox',
				}
			} ] ],
			data: medicinePrescriptions,
			toolbar : '#toolbar'
		});
		$('#dataGrid').datagrid('enableCellEditing');
		debugger;

	});
	
	function clearSelected() {
		$('#dataGrid').datagrid('clearSelections');
	}
	
	function add() {
		var medicinePrescriptions = $('#dataGrid').datagrid('getSelections');
		if(medicinePrescriptions.length == 0) {
			$.messager.alert('错误', '未选择药品', 'error');
		} else {
			debugger;
			var registerId = "${registerId}";
			$.ajax({
				type: "POST",
				async: false,
				data:  {
					'medicinePrescriptionStr':JSON.stringify(medicinePrescriptions),
					'registerId':registerId,
				},
		        url: '${ctx}/registerMedicine/addPrescription',
		        dataType: "json",
		        success:function(result) {
		        	if(result.status.errorCode == 0) {
		        		$.messager.alert('成功', result.status.errorMsg, 'info',function(r){
		        			parent.$.modalDialog.handler.dialog('refresh','${ctx}/registerMedicine/detailPage?registerId=' + registerId);
		        		});
		        	} else {
		        		$.messager.alert('错误', result.status.errorMsg, 'error');
		        	}
		        },
		        error:function(result) {
		        }
	   	 	});
		}
	}
	
	function charge() {
		var registerId = "${registerId}";
		$.ajax({
			type: "POST",
			async: false,
			data:  {
				'registerId':registerId
			},
	        url: '${ctx}/registerMedicine/charge',
	        dataType: "json",
	        success:function(result) {
	        	if(result.status.errorCode == 0) {
	        		$.messager.alert('成功', result.status.errorMsg, 'info',function(r){
	        			parent.$.modalDialog.handler.dialog('refresh','${ctx}/registerMedicine/detailPage?registerId=' + registerId);
	        		});
	        	} else {
	        		$.messager.alert('错误', result.status.errorMsg, 'error');
	        	}
	        },
	        error:function(result) {
	        	$.messager.alert('错误', result, 'error');
	        }
   	 	});
	}
	
	function takeMedicine() {
		var registerId = "${registerId}";
		$.ajax({
			type: "POST",
			async: false,
			data:  {
				'registerId':registerId
			},
	        url: '${ctx}/registerMedicine/takeMedicine',
	        dataType: "json",
	        success:function(result) {
	        	if(result.status.errorCode == 0) {
	        		$.messager.alert('成功', result.status.errorMsg, 'info',function(r){
	        			parent.$.modalDialog.handler.dialog('refresh','${ctx}/registerMedicine/detailPage?registerId=' + registerId);
	        		});
	        	} else {
	        		$.messager.alert('错误', result.status.errorMsg, 'error');
	        	}
	        },
	        error:function(result) {
	        }
   	 	});
		
	}
</script>
</head>

<div class="easyui-layout" data-options="fit:true,border:false">
	
	<div data-options="region:'center',fit:true,border:false" title="开药时间${addTime }; &nbsp;&nbsp; 缴费时间${chargeTime}; &nbsp;&nbsp; 取药时间 ${medicineTime};">
		<table id="dataGrid" data-options="fit:true,border:false"></table>
	</div>
	
	<div id="toolbar" style="display: none;">
			<c:if test="${addTime == null }">
				<c:if test="${fn:contains(sessionInfo.resourceList, '/registerMedicine/addPrescription')}">
				
				<a onclick="add();" href="javascript:void(0);"
					class="easyui-linkbutton"
					data-options="plain:true,iconCls:'icon_add'">开单</a>
					
				<a onclick="clearSelected();" href="javascript:void(0);"
					class="easyui-linkbutton"
					data-options="plain:true,iconCls:'icon_del'">清除选择</a>
				</c:if>
				
			</c:if>
			
			<c:if test="${addTime != null and chargeTime == null }">
				<c:if test="${fn:contains(sessionInfo.resourceList, '/registerMedicine/charge')}">
					<a onclick="charge();" href="javascript:void(0);"
						class="easyui-linkbutton"
						data-options="plain:true,iconCls:'icon_add'">缴费</a>
				</c:if>
			</c:if>
			
			<c:if test="${chargeTime != null and medicineTime == null }">	
				<c:if test="${fn:contains(sessionInfo.resourceList, '/registerMedicine/takeMedicine')}">
					<a onclick="takeMedicine();" href="javascript:void(0);"
						class="easyui-linkbutton"
						data-options="plain:true,iconCls:'icon_add'">取药</a>
				</c:if>
			</c:if>
	</div>
</div>
